package cn.tedu.property_management_system.service.Impl;

import cn.tedu.property_management_system.cache.repo.IRedisCache;
import cn.tedu.property_management_system.entity.House;
import cn.tedu.property_management_system.entity.Owner;
import cn.tedu.property_management_system.entity.VO.OwnerVO;
import cn.tedu.property_management_system.entity.dto.MeterSO;
import cn.tedu.property_management_system.entity.dto.OwnerDTO;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.mapper.HouseMapper;
import cn.tedu.property_management_system.mapper.OwnerMapper;
import cn.tedu.property_management_system.service.IOwnerService;
import cn.tedu.property_management_system.utils.JsonPage;
import cn.tedu.property_management_system.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.corba.se.impl.orb.ORBVersionImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class OwnerServiceImpl implements IOwnerService {

    @Autowired
    OwnerMapper ownerMapper;

    @Autowired
    HouseMapper houseMapper;


    @Override
    public int addOwner(OwnerDTO ownerDTO) {

        House house = houseMapper.getByRoom(ownerDTO.getRoom());
        if (house == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "房间不存在");
        }
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerDTO, owner);
        owner.setRoomId(house.getId());
        owner.setTime1(System.currentTimeMillis());
        int row = ownerMapper.insertOwner(owner);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "添加业主信息失败");
        }

        return row;
    }

    @Override
    public int deleteOwner(Integer id) {
        OwnerVO ownerVO = ownerMapper.getById(id);
        if (ownerVO == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "业主不存在,删除失败");
        }
        int row = ownerMapper.deleteOwner(id);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "删除异常,请重试");
        }
        log.debug(row + "------01");
        return row;
    }

    @Override
    public int updateOwner(OwnerVO ownerVO) {
        OwnerVO ownerVO1 = ownerMapper.getById(ownerVO.getId());
        if (ownerVO1 == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该业主不存在,修改失败");
        }
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerVO, owner);
        House house = houseMapper.getByRoom(ownerVO1.getRoom());
        if (house == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "房屋编号不存在,请选择正确的房屋");
        }
        owner.setRoomId(house.getId());
        owner.setTime1(System.currentTimeMillis());
        int row = ownerMapper.updateOwner(owner);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "修改失败,请重试");
        }

        return row;
    }

    @Override
    public List<OwnerVO> getOwnerVO() {
        List<Serializable> serializables = getCaches();

        List<OwnerVO> ownerVOS = new ArrayList<>();

        if (serializables.size() > 1) {
            for (Serializable serializable : serializables) {
                if (serializable instanceof OwnerVO)
                    ownerVOS.add((OwnerVO) serializable);
            }
        }
        ownerVOS = ownerMapper.getListOwnerVO();
        if (ownerVOS.size() < 1) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "数据不存在------------");
        }
        return ownerVOS;
    }

    @Override
    public List<OwnerVO> getOwnerVOByTelephonAndIdNumber(String telephon, String idNumber) {
        String key = "owner" + telephon + idNumber;
        List<OwnerVO> list = new ArrayList<>();

        Serializable serializable = redisCache.getACache(key);
        if (serializable != null) {
            if (serializable instanceof OwnerVO)
                list.add((OwnerVO) serializable);
            return list;

        }

        OwnerVO ownerVOS = ownerMapper.getOwnerVOByTelephoneAndIdNumber(telephon, idNumber);
        if (ownerVOS == null) {
            redisCache.addACache(key, null);
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该数据不存在");
        }
        list.add(ownerVOS);

        return list;
    }

    @Override
    public void startCache() {
        loadRedis();

        for (Serializable serializable:getCaches()){
            log.debug("--------"+serializable.toString() );
        }

    }

    @Override
    public OwnerVO getById(Integer id) {

        return ownerMapper.getById(id);
    }

    @Override
    public int deletes(List<OwnerVO> ids) {
        for (OwnerVO ownerVO : ids) {
            ownerMapper.deleteOwner(ownerVO.getId());
        }
        return 1;
    }

    @Override
    public List<String> getOwnerName() {
        List<OwnerVO> ownerVOS = getOwnerVO();
        List<String> list = new ArrayList<>();
        for (OwnerVO ownerVO:ownerVOS){
            list.add(ownerVO.getOwnerName());
        }
        return list;
    }


    @Autowired
    IRedisCache redisCache;


    private void loadRedis() {

        String key = "owners";
        List<OwnerVO> ownerVOS = ownerMapper.getListOwnerVO();
        List<Serializable> serializables = new ArrayList<>();
        for (OwnerVO ownerVO : ownerVOS) {
            serializables.add(ownerVO);
        }
        redisCache.clear(key);
        redisCache.addCaches(key, serializables);

    }

    private List<Serializable> getCaches() {
        String key = "owners";
        return redisCache.getCaches(key);
    }


    public JsonPage<OwnerVO> pageSelect(Integer pageSize, Integer pageIndex){
        PageHelper.startPage(pageSize,pageIndex);
        List<OwnerVO> list=ownerMapper.getListOwnerVO();
        return JsonPage.restPage(new PageInfo<>(list));
    }


}
